import sys
import re
import random

sys.path.append('./')
sys.path.append('/Users/xuchaosheng/anaconda3/lib/python3.6/site-packages')

from config import *

import neo4j_port as kg


@app.route('/trainPlatform/knowledge_items.do', methods = ['GET'])
def knowledge_items():
    kw = request.args.get('kw', '').strip()

    if kw:
        data = kg.KeyWrodSearch(kw)
    else:
        data = kg.FindRoots()

    if isinstance(data, tuple) and not data[0]:
        resp = {
            'success': False,
            'msg': data[1]
        }

        response = make_response(jsonify(resp))
        return response

    data = list(data)
    data.sort(key = lambda i: i, reverse = True)

    resp = {
        'success': True,
        'data': data
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/knowledge_search.do', methods = ['GET'])
def knowledge_search():
    key = request.args.get('key', '').strip()
    resp = kg.RelativesOf(key)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'data': resp
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/update_node_name.do', methods = ['GET'])
def update_node_name():
    oldname = request.args.get('oldname', '').strip()
    newname = request.args.get('newname', '').strip()

    resp = kg.ModifyNode(oldname, newname)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/insert_node.do', methods = ['GET'])
def insert_node():
    root = request.args.get('root', '').strip()
    leaf = request.args.get('leaf', '').strip()
    rltp = request.args.get('rltp', '').strip()

    resp = kg.NewRelation(root, leaf, rltp)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/delete_node.do', methods = ['GET'])
def delete_node():
    root = request.args.get('root', '').strip()
    leaf = request.args.get('leaf', '').strip()

    if not root or not leaf:
        resp = {
            'success': False,
            'msg': 'root or leaf is not permitted empty'
        }

        response = make_response(jsonify(resp))
        return response

    resp = kg.DeleteRelation(root, leaf)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/addSynonym.do', methods = ['GET'])
def addSynonym():
    root = request.args.get('root', '').strip()
    leaf = request.args.get('leaf', '').strip()
    rltp = 'SynonymOf'

    if not root or not leaf:
        resp = {
            'success': False,
            'msg': 'root or leaf is not permitted empty'
        }

        response = make_response(jsonify(resp))
        return response

    resp = kg.NewRelationAndMerge(root, leaf, rltp)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/delSynonym.do', methods = ['GET'])
def delSynonym():
    root = request.args.get('root', '').strip()
    leaf = request.args.get('leaf', '').strip()

    if not root or not leaf:
        resp = {
            'success': False,
            'msg': 'root or leaf is not permitted empty'
        }

        response = make_response(jsonify(resp))
        return response

    resp = kg.DeleteRelation(root, leaf)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/newNode.do', methods = ['GET'])
def newNode():
    name = request.args.get('name', '').strip()

    if not name:
        resp = {
            'success': False,
            'msg': 'name is not permitted empty'
        }

        response = make_response(jsonify(resp))
        return response

    resp = kg.NewNode(name)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/delNode.do', methods = ['GET'])
def delNode():
    name = request.args.get('name', '').strip()

    if not name:
        resp = {
            'success': False,
            'msg': 'name is not permitted empty'
        }

        response = make_response(jsonify(resp))
        return response

    resp = kg.DeleteNode(name)

    if isinstance(resp, tuple) and not resp[0]:
        resp = {
            'success': False,
            'msg': resp[1]
        }

        response = make_response(jsonify(resp))
        return response

    resp = {
        'success': True,
        'msg': 'success'
    }

    response = make_response(jsonify(resp))
    return response
